package cn.zswltech.gruul.auth;

import cn.zswltech.gruul.biz.service.LoginService;
import cn.zswltech.gruul.biz.service.UserService;
import cn.zswltech.gruul.common.entity.DisplayMenuTreeDO;
import cn.zswltech.gruul.common.entity.UserDO;
import cn.zswltech.gruul.common.result.MSG;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.domain.entity.CheckResult;
import cn.zswltech.gruul.web.api.util.CookieUtils;
import cn.zswltech.gruul.web.api.util.JwtUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author Jim
 * @version 1.0.0
 * @descripition: 审计系统权限校验service
 * @date 2024/2/2 14:27
 */
@Service
@Slf4j
public class SJApplicationAuthServiceImpl implements ThirdApplicationAuthService {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginService loginService;

    @Override
    public Response<DisplayMenuTreeDO> menuTree() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
         if (testAccount(request)){
             UserDO queryDO = new UserDO();
             queryDO.setAccount(request.getHeader("test-account"));
             log.info("免校验账号：{}",request.getHeader("test-account") );
             UserDO loginUser = userService.selectOne(queryDO);;
             return loginService.getMenuTrees(loginUser);
         }
        String sjToken = CookieUtils.getSJToken(request);
        log.info("API /user/menuTree sjtoken:" + sjToken);

        if (StringUtils.isBlank(sjToken)) {
            return Response.error(MSG.req_error_token_null);
        }
        //解析token
        JSONObject jsonObject = JSONObject.parseObject(sjToken);
        String token = jsonObject.getString(CookieUtils.WL_TOKEN);
        CheckResult checkResult = JwtUtils.validateJWT(token);

        if (checkResult.isSuccess()) {
            // 账号
            String account = (String) checkResult.getClaims().get("sub");
            UserDO queryDO = new UserDO();
            queryDO.setAccount(account);
            UserDO loginUser = userService.selectOne(queryDO);
            if (loginUser == null) {
                return Response.error(MSG.req_error_account_or_password_failed);
            }
            //true 要修改密码， false 不需要修改密码
            if (loginUser.getUpdatePwdTime() != null) {
                loginUser.setUpdatePwdStatus(loginUser.getUpdatePwdTime().before(new Date()));
            }
            return loginService.getMenuTrees(loginUser);
        } else {
            return Response.error(1, "用户权限查询失败");
        }
    }

    private boolean testAccount(HttpServletRequest request) {
        String testAccount = request.getHeader("test-account");
        if (testAccount != null) {
            return true;
        }
        return false;
    }
}
